#import "PageImageView.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "ImagePreview.h"

@interface PageImageView()
{
    UIScrollView    *scrollView;
//    UIPageControl   *pageControll;
    UIImageView *shadeImageView;
    UILabel *titleLabel;
    UILabel *pageLabel;
    
    UIButton *backButton;
    
    UIScrollView *largeScroll;
    UIView *textView;
    UITextView *descrTextView;
    UILabel *descrTitleLabel;
    UILabel *descrPageLabel;
    
    UIView *bottomView;
}

@property (strong, nonatomic) NSArray *aryImages;
@property (strong, nonatomic) NSArray *aryTitles;
@property (strong, nonatomic) NSArray *aryDescrs;

@end

@implementation PageImageView

- (id)initWithFrame:(CGRect)frame WithImages:(NSArray *)imgs AndTitles:(NSArray *)titles AndDescr:(NSArray *)descrs
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        // Initialization code
        self.aryImages = imgs;
        self.aryTitles = titles;
        self.aryDescrs = descrs;
        
        scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(ScreenWidth*[imgs count], frame.size.height);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.tag = 99990;
        scrollView.delegate = self;
        
        for (int i=0; i<[imgs count]; i++) {
            UIButton *imgView = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, frame.size.height)];
            imgView.backgroundColor = [UIColor clearColor];
            imgView.tag = 1000+i;
                        [imgView setContentMode:UIViewContentModeCenter];
            [imgView sd_setImageWithURL:[NSURL URLWithString:imgs[i]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"building_detail_layout_default.png"]];
            [imgView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
            [scrollView addSubview:imgView];
            
//            UIImageView *shadeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"building_layout_shade.png"]];
//            shadeImageView.frame = CGRectMake(ScreenWidth*i, frame.size.height - 25, 320, 25);
//            [scrollView addSubview:shadeImageView];
//            
//            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 310, 25)];
//            titleLabel.backgroundColor = [UIColor clearColor];
//            titleLabel.font = [UIFont systemFontOfSize:13];
//            titleLabel.textColor = [UIColor whiteColor];
//            titleLabel.text = self.aryTitles[i];
//            [shadeImageView addSubview:titleLabel];
        }
        
//        pageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height-30, 320, 30)];
//        pageControll.numberOfPages = [imgs count];
//        pageControll.currentPage = 0;
//        pageControll.hidesForSinglePage = YES;
//        pageControll.currentPageIndicatorTintColor = [UIColor colorWithHexString:@"#f04137"];
//        pageControll.pageIndicatorTintColor = [UIColor grayColor];
//
//        [pageControll addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
        
        [self addSubview:scrollView];
//        [self addSubview:pageControll];
        
        shadeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"building_layout_shade.png"]];
        shadeImageView.frame = CGRectMake(0, frame.size.height - 25, 320, 25);
        [self addSubview:shadeImageView];

        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 280, 25)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.text = self.aryTitles[0];
        [shadeImageView addSubview:titleLabel];
        
        pageLabel = [[UILabel alloc] initWithFrame:CGRectMake(280, 0, 40, 25)];
        pageLabel.font = [UIFont systemFontOfSize:13];
        pageLabel.textColor = [UIColor whiteColor];
        pageLabel.text = [NSString stringWithFormat:@"1/%i", (int)self.aryImages.count];
        [shadeImageView addSubview:pageLabel];
        
        [self addSubview:shadeImageView];
    }
    return self;
}

int cur_page = 0;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender
{
    CGFloat pageWidth = sender.frame.size.width;
    int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    if (sender.tag == 99990) {
        if (page < self.aryTitles.count) {
            titleLabel.text = self.aryTitles[page];
        } else {
            titleLabel.text = @"";
        }
        
        pageLabel.text = [NSString stringWithFormat:@"%i/%i", page + 1, (int)self.aryImages.count];
//        pageControll.currentPage = page;
    } else {
        if (cur_page == page) {
            return;
        }
        UIView *view = [sender viewWithTag:(91127 + cur_page)];
        if ([view isKindOfClass:[ImagePreview class]]) {
            ImagePreview *ipv = (ImagePreview *)view;
            [ipv returnScale];
        }
        cur_page = page;
        if (page < self.aryDescrs.count) {
            descrTextView.text = self.aryDescrs[page];
        } else {
            descrTextView.text = nil;
        }
        if (page < self.aryTitles.count) {
            descrTitleLabel.text = self.aryTitles[page];
        } else {
            descrTitleLabel.text = nil;
        }
        descrPageLabel.text = [NSString stringWithFormat:@"%i/%i", page + 1, (int)self.aryImages.count];
    }
}

//- (void)changePage:(id)sender {
//    int page = (int)pageControll.currentPage;
//    
//    // update the scroll view to the appropriate page
//    CGRect frame = scrollView.frame;
//    frame.origin.x = frame.size.width * page;
//    frame.origin.y = 0;
//    [scrollView scrollRectToVisible:frame animated:YES];
//}

- (void)tapAction:(UIButton *)sender
{
    int tag = (int)sender.tag-1000;
    
    self.aryImages = self.aryLayoutImages;
    self.aryTitles = self.aryLayoutTitles;
    self.aryDescrs = self.aryLayoutDescrs;
    
    largeScroll = [self getLargeScrollView];
    
    descrTextView.text = self.aryDescrs[tag];
    descrTitleLabel.text = self.aryTitles[tag];
    descrPageLabel.text = [NSString stringWithFormat:@"%i/%i", tag + 1, (int)self.aryImages.count];
    
    
    CGRect frame = largeScroll.frame;
    frame.origin.x = frame.size.width * tag;
    frame.origin.y = 0;
    cur_page = tag;
    [largeScroll scrollRectToVisible:frame animated:YES];
    
    bottomView = [self getBottomView];
    
    backButton = [self getBackButton];
    
    [theApp.window addSubview:largeScroll];
    [theApp.window addSubview:textView];
    [theApp.window addSubview:bottomView];
    [theApp.window addSubview:backButton];
}

- (UIButton *)getBackButton {
    if (backButton == nil) {
        backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setImage:[UIImage imageNamed:@"building_detail_layout_back.png"] forState:UIControlStateNormal];
        backButton.showsTouchWhenHighlighted = NO;
        backButton.frame = CGRectMake(3, 22, 40, 40);
        [backButton addTarget:self action:@selector(dismissAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return backButton;
}

- (UIScrollView *)getLargeScrollView {
    largeScroll = [[UIScrollView alloc] initWithFrame:theApp.window.bounds];
    largeScroll.backgroundColor = [UIColor colorWithHexString:@"#F0F0F0"];
    largeScroll.showsVerticalScrollIndicator = NO;
    largeScroll.showsHorizontalScrollIndicator = NO;
    largeScroll.contentSize = CGSizeMake(ScreenWidth*[self.aryImages count], ScreenHeight);
    largeScroll.pagingEnabled = YES;
    largeScroll.tag = 99991;
    largeScroll.delegate = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideSomething)];
    [largeScroll addGestureRecognizer:tap];
    
    if (self.aryImages == nil || self.aryImages.count == 0) {
        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"building_detail_layout_default.png"]];
        img.size = CGSizeMake(ScreenWidth, 200);
//        img.origin = CGPointMake(0, textView.origin.y - img.size.height - 10);
        img.center = largeScroll.center;
        [largeScroll addSubview:img];
    } else {
        for (int i=0; i<[self.aryImages count]; i++) {
            ImagePreview *imagePreview = [[ImagePreview alloc] initWithFrame:CGRectMake(ScreenWidth*i, 0, ScreenWidth, ScreenHeight) Url:self.aryImages[i]];
            imagePreview.tag = 91127 + i;
//            [imagePreview.imgView sd_setImageWithURL:[NSURL URLWithString:self.aryImages[i]] placeholderImage:[UIImage imageNamed:@"building_detail_layout_default.png"]];
//            imagePreview.imgView.size = CGSizeMake(320, 200);
//            imagePreview.imgView.origin = CGPointMake(0, textView.origin.y - imagePreview.imgView.size.height - 10);
            [largeScroll addSubview:imagePreview];
            [tap requireGestureRecognizerToFail:imagePreview.tapImgViewTwice];
        }
    }
    
    
    int textViewHeight = ScreenHeight - 50 - largeScroll.center.y - 100 - 5;
    textView = [[UIView alloc] initWithFrame:CGRectMake(0, largeScroll.center.y + 100 + 5, ScreenWidth, textViewHeight)];
    textView.backgroundColor = [UIColor clearColor];
    
    descrTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 270, 15)];
    descrTitleLabel.font = [UIFont boldSystemFontOfSize:14];
    descrTitleLabel.textColor = [UIColor blackColor];
    [textView addSubview:descrTitleLabel];
    descrPageLabel = [[UILabel alloc] initWithFrame:CGRectMake(descrTitleLabel.origin.x + descrTitleLabel.width, descrTitleLabel.origin.y, 38, 15)];
    descrPageLabel.font = [UIFont systemFontOfSize:14];
    descrPageLabel.textColor = [UIColor blackColor];
    descrPageLabel.textAlignment = NSTextAlignmentRight;
    [textView addSubview:descrPageLabel];
    
    
    descrTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, descrTitleLabel.origin.y + descrTitleLabel.size.height, textView.width, textViewHeight - descrTitleLabel.size.height - 10)];
    descrTextView.backgroundColor = [UIColor clearColor];
    descrTextView.textColor = [UIColor blackColor];
    descrTextView.editable = NO;
    descrTextView.showsVerticalScrollIndicator = YES;
    [textView addSubview:descrTextView];
    
    
    return largeScroll;
}

- (UIView *)getBottomView {
    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 50, 320, 50)];
    bottomView.backgroundColor = [UIColor colorWithHexString:@"#5D5B5A"];
    UIButton *layoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    layoutButton.exclusiveTouch = YES;
    layoutButton.tag = 991202;
    [layoutButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_normal.png"] forState:UIControlStateNormal];
    [layoutButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_down.png"] forState:UIControlStateHighlighted];
    [layoutButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_down.png"] forState:UIControlStateSelected];
    layoutButton.frame = CGRectMake(0, 0, 106, 50);
    [layoutButton setTitle:@"户型图" forState:UIControlStateNormal];
    [layoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [layoutButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    layoutButton.selected = YES;
    [bottomView addSubview:layoutButton];
    
    UIButton *trafficButton = [UIButton buttonWithType:UIButtonTypeCustom];
    trafficButton.exclusiveTouch = YES;
    trafficButton.tag = 991203;
    [trafficButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_normal.png"] forState:UIControlStateNormal];
    [trafficButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_down.png"] forState:UIControlStateHighlighted];
    [trafficButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_down.png"] forState:UIControlStateSelected];
    trafficButton.frame = CGRectMake(107, 0, 106, 50);
    [trafficButton setTitle:@"交通图" forState:UIControlStateNormal];
    [trafficButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [trafficButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:trafficButton];
    
    UIButton *effectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    effectButton.exclusiveTouch = YES;
    effectButton.tag = 991204;
    [effectButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_normal.png"] forState:UIControlStateNormal];
    [effectButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_down.png"] forState:UIControlStateHighlighted];
    [effectButton setBackgroundImage:[UIImage imageNamed:@"building_detail_layout_button_down.png"] forState:UIControlStateSelected];
    effectButton.frame = CGRectMake(214, 0, 106, 50);
    [effectButton setTitle:@"效果图" forState:UIControlStateNormal];
    [effectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [effectButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:effectButton];
    
    return bottomView;
}

- (void)buttonClick:(UIButton *)sender {
    UIButton *btn1 = (UIButton *)[bottomView viewWithTag:991202];
    UIButton *btn2 = (UIButton *)[bottomView viewWithTag:991203];
    UIButton *btn3 = (UIButton *)[bottomView viewWithTag:991204];
    btn1.selected = NO;
    btn2.selected = NO;
    btn3.selected = NO;
    
    // 户型图
    if (sender.tag == 991202) {
        self.aryImages = [self.aryLayoutImages copy];
        self.aryTitles = [self.aryLayoutTitles copy];
        self.aryDescrs = [self.aryLayoutDescrs copy];
    }
    // 交通图
    else if (sender.tag == 991203) {
        self.aryImages = [self.aryTrafficImages copy];
        self.aryTitles = [self.aryTrafficTitles copy];
        self.aryDescrs = [self.aryTrafficDescrs copy];
    }
    // 效果图
    else if (sender.tag == 991204) {
        self.aryImages = [self.aryEffectImages copy];
        self.aryTitles = [self.aryEffectTitles copy];
        self.aryDescrs = [self.aryEffectDescrs copy];
    }
    sender.selected = YES;
    [largeScroll removeFromSuperview];
    [textView removeFromSuperview];
    [bottomView removeFromSuperview];
    largeScroll = [self getLargeScrollView];
    
    if (self.aryDescrs == nil || self.aryDescrs.count == 0) {
        descrPageLabel.text = [NSString stringWithFormat:@"0/0"];
    } else {
        descrTextView.text = self.aryDescrs[0];
        descrTitleLabel.text = self.aryTitles[0];
        descrPageLabel.text = [NSString stringWithFormat:@"1/%i", (int)self.aryImages.count];
    }
    cur_page = 0;
    [theApp.window addSubview:largeScroll];
    [theApp.window addSubview:textView];
    [theApp.window addSubview:bottomView];
    [theApp.window addSubview:backButton];
}

- (void)dismissAction:(id)sender
{
    UIView *view = [largeScroll viewWithTag:(91127 + cur_page)];
    if ([view isKindOfClass:[ImagePreview class]]) {
        ImagePreview *ipv = (ImagePreview *)view;
        [ipv returnScale];
    }
    [largeScroll removeFromSuperview];
    [textView removeFromSuperview];
    [backButton removeFromSuperview];
    [bottomView removeFromSuperview];
}

- (void)hideSomething {
    UIColor *currentColor = largeScroll.backgroundColor;
    UIColor *color = nil;
    if([currentColor isEqual:[UIColor blackColor]]) {
        color = [UIColor colorWithHexString:@"#F0F0F0"];
    } else {
        color = [UIColor blackColor];
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.5];
    largeScroll.backgroundColor = color;
    textView.hidden = !textView.hidden;
    backButton.hidden = !backButton.hidden;
    bottomView.hidden = !bottomView.hidden;
    [UIView commitAnimations];
}

@end
